﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#if 0
typedef struct PersonStruct
{
	char name[64];
	int age;
}MyPerson;

//typedef struct PersonStruct MyPerson;
#endif

struct PersonConstruct
{
	char name[64];
	int age;
}personConstruct = { "KonwKing",83 };

struct
{
	char name[64];
	int age;
}Obama = { "BlackGod",65 };

void PrintPersons(struct PersonConstruct* persons, int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("9.输出栈上结构体数组persons[i]的name为：%s，age为：%d\n", persons[i].name, persons[i].age);
		//aaa 20，...，fff 70
	}
}
void testConstructBasicConcept()
{	//6.单个结构体变量；
	//strcpy(personConstruct.name, "Jack");
	//personConstruct.age = 27;
	printf("1.输出结构体personConstruct的成员name为：%s 以及age为：%d\n", personConstruct.name, personConstruct.age);
	//Jack  27		-->   KonwKing  83

	//2.在栈上分配结构体变量空间；
	struct PersonConstruct person = { "LiMing",28 };
	printf("3.栈上分配的结构体变量person的成员name为：%s，及age为：%d\n", person.name, person.age);//LiMing  28

	//4.在堆上分配结构体变量空间；
	struct PersonConstruct* personHeap = malloc(sizeof(struct PersonConstruct));
	strcpy(personHeap->name, "LiLei");
	personHeap->age = 27;
	printf("5.堆上分配的结构体变量personHeap的成员name为：%s，及age为：%d\n", personHeap->name, personHeap->age);
	//LiLei  27

	//7.多个结构体变量；
	//8.在栈上分配结构体数组空间；
	struct PersonConstruct persons[] = {
		{"aaa",20},
		{"bbb",30},
		{"ccc",40},
		{"ddd",50},
		{"eee",60},
		{"fff",70},
	};
	int len = sizeof(persons) / sizeof(struct PersonConstruct);

	PrintPersons(persons, len);

	//10.在堆上分配结构体数组空间；
	struct PersonConstruct* personHeaps = malloc(sizeof(struct PersonConstruct) * 6);
	for (int i = 0; i < 6; ++i)
	{
		//11.将字符串name_%d封装到结构体成员name中；
		sprintf(personHeaps[i].name, "name_%d", i + 1);
		personHeaps[i].age = 100 + i;
	}
	PrintPersons(personHeaps, 6);//name_1  100，...，name_6  105
}

//main205：结构体的基本概念定义初始化赋值
int main205()
{
	testConstructBasicConcept();

	system("pause");
	return EXIT_SUCCESS;
}